<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/7/5
 * Time: 下午4:31
 */

namespace Superadmin\Controller;


use Org\Util\Tree;
use Think\Exception;

class MenuController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $this->_mod = D('menu');
    }

    //菜单列表
    public function getlist()
    {
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->_mod->where(array('display' => 1))->order('ordid')->select();
        $array = array();
        foreach($result as $r) {
            $r['cname'] = L($r['name']);
            $r['str_manage'] = '<a href="'.U('superadmin/menu/add',array('pid'=>$r['id'])).'">'.L('add_submenu').'</a> |
                                <a href="'.U('superadmin/menu/edit',array('id'=>$r['id'])).'">'.L('edit').'</a> |
                                <a href="javascript:;" class="layer_confirm"  data-url="'.U('superadmin/menu/delete',array('id'=>$r['id'])).'" data-msg="'.sprintf(L('confirm_delete_one'),$r['name']).'">'.L('delete').'</a>';
            $array[] = $r;
        }
        $str  = "<tr>
                <td align='center'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td align='center'>\$id</td>
                <td>\$spacer<span data-tdtype='edit' data-field='name' data-id='\$id' class='tdedit'>\$name</span></td>
                <td align='left'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>
                <td align='center'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $menu_list = $tree->get_tree(0, $str);
        $this->assign('menu_list', $menu_list);

        $big_menu = array(
            'title' => '添加菜单',
            'uri' => U('menu/add'),
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }


    //菜单add
    public function add()
    {
        if(IS_AJAX)
        {
            $post = array(
                'name' => I('post.menuname'),
                'pid' => I('post.pid'),
                'module_name' =>  I('post.controlname'),
                'action_name' => I('post.actionname'),
                'data' => I('post.data'),
                'remark' => I('post.remark'),
                'ordid' => I('post.ordid'),
                'display' => I('post.display'),
            );
            if(!$post['name'])
            {
                $data = array(
                    'status' => 0,
                    'msg'    => '请输入菜单名称'
                );
                $this->ajaxReturn($data);
            }
            if($post['pid'] === '')
            {
                $data = array(
                    'status' => 0,
                    'msg'    => '请选择父级菜单'
                );
                $this->ajaxReturn($data);
            }
            if(!$post['module_name'])
            {
                $data = array(
                    'status' => 0,
                    'msg'    => '请输入控制器名称'
                );
                $this->ajaxReturn($data);
            }
            if(!$post['action_name'])
            {
                $data = array(
                    'status' => 0,
                    'msg'    => '请输入方法名'
                );
                $this->ajaxReturn($data);
            }
           if(!D('menu')->create($post))
           {
               $msg = D('menu')->getError();
               $data = array(
                   'status' => 0,
                   'msg'    => $msg
               );
               $this->ajaxReturn($data);
           }
            D('menu')->add($post);
            $data = array(
                'status' => 1,
                'msg'    => L('operation_success'),
            );
            $this->ajaxReturn($data);
        }
        $tree = new Tree();
        $result = $this->_mod->where(array('display' => 1))->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $_GET['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
        $this->display();
    }



    //菜单删除
    public function delete()
    {
        $id = I('get.id');
        if(I('post.id'))
        {
            $id = I('post.id');
        }
        if(strpos($id,','))
        {
            $field = I('post.field');
            $where = array(
                $field => array('in', $id)
            );
        }
        if(!isset($where))
        {
            $find = D('menu')->find(intval($id));
        } else {
            $find = D('menu')->where($where)->select();
        }
        if(!$find)
        {
            $data = array(
                'status' => 0,
                'msg'    => '数据不存在'
            );
            $this->ajaxReturn($data);
        }
        try{
           if(!isset($where)) $where  = array('id' => $id);
           $delete = array('display' => 0);
          if(D('menu')->delete($where, $delete))
          {
              $data = array(
                  'status' => 1,
                  'msg'    => L('operation_success'),
              );
          }
        } catch(Exception $e)
        {
            $msg = $e->getMessage();
            $data = array(
                'status' => 0,
                'msg'    => $msg
            );
        }
        $this->ajaxReturn($data);
    }


    //菜单edit
    public function edit()
    {
        if(IS_AJAX)
        {
            $id   = I('post.menuid');
            $post = array(
                'name' => I('post.menuname'),
                'pid' => I('post.pid'),
                'module_name' =>  I('post.controlname'),
                'action_name' => I('post.actionname'),
                'data' => I('post.data'),
                'remark' => I('post.remark'),
                'ordid' => I('post.ordid'),
                'display' => I('post.display'),
            );
            $menu = D('menu')->find($id);
            if(!$menu)
            {
                $msg = array('status' =>0, 'msg' => '数据不存在');
                $this->ajaxReturn($msg);
            }
            //update
            try{
                D('menu')->where(array('id'=> $id))->save($post);
            } catch (Exception $e)
            {
                $msg = array('status' =>0, 'msg' => $e->getMessage());
                $this->ajaxReturn($msg);
            }
            $msg = array('status' =>1, 'msg' => L('operation_success'));
            $this->ajaxReturn($msg);

        }
        $id = I('get.id','intval');
        $menu = D('menu')->find($id);
        if(!$menu)
        {
            $this->error('数据不存在', u('superadmin/menu/getlist'));
            exit;
        }
        $tree = new Tree();
        $result = D('menu')->select();
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $menu['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
        $this->assign('menu', $menu);
        $this->display();
    }



}